<template>
	<view class="container">
		<!-- 小程序头部兼容 -->
		<!-- #ifdef MP -->
		<view class="mp-search-box">
			<input class="ser-input" type="text" value="输入关键字搜索" disabled />
		</view>
		<!-- #endif -->

		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<!-- 背景色区域 -->
			<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
			<swiper class="carousel" circular @change="swiperChange">
				<swiper-item v-for="(item, index) in advertiseList" :key="index" class="carousel-item"
					@click="navToAdvertisePage(item)">
					<image :src="item.pic" />
				</swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots">
				<text class="num">{{swiperCurrent+1}}</text>
				<text class="sign">/</text>
				<text class="num">{{swiperLength}}</text>
			</view>
		</view>


		<!-- 秒杀专区 -->
		<!-- 		<view class="f-header m-t" v-if="homeFlashPromotion!==null">
			<image src="/static/icon_flash_promotion.png"></image>
			<view class="tit-box">
				<text class="tit">秒杀专区</text>
				<text class="tit2">下一场 {{homeFlashPromotion.nextStartTime | formatTime}} 开始</text>
			</view>
			<view class="tit-box">
				<text class="tit2" style="text-align: right;">本场结束剩余：</text>
				<view style="text-align: right;">
					<text class="hour timer">{{cutDownTime.endHour}}</text>
					<text>:</text>
					<text class="minute timer">{{cutDownTime.endMinute}}</text>
					<text>:</text>
					<text class="second timer">{{cutDownTime.endSecond}}</text>
				</view>
			</view>
			<text class="yticon icon-you" v-show="false"></text>
		</view> -->

		<!-- <product-list-switch :type="listModel"  :productList="homeFlashPromotion.productList"/> -->

		<view class="hot-section">
			<view v-for="(item, index) in allProductList" :key="index" class="guess-item"
				@click="navToDetailPage(item.product)">
				<view class="image-wrapper">
					<image :src="item.product.pic" mode="aspectFill"></image>
				</view>
				<view class="txt">
					<text class="title clamp">{{item.product.name}}</text>
					<text class="title2">{{item.product.subTitle}}</text>
					<text class="price">￥{{item.product.price}}</text>
					<view class="selector-wrapper">
						<block v-if="item.skuStockList.length > 1">
							<text class="choose-btn">选规格</text>
							<view v-if="item.quantity > 0" class="circle-num">{{item.quantity}}</view>
						</block>

						<uni-number-box v-else class="step" :min="1" :max="100" :value="item.quantity" :index="index"
							@eventChange="numberChange" />
					</view>
				</view>

			</view>
		</view>
		<HalfScreenPopup class="Popup" :show="showPopup" @update:show="showPopup = false">
			<view>
				<text>这里是弹出的内容！！</text>
			</view>
		</HalfScreenPopup>

		<view class="floatCartView">

			<view class="floatCartViewMain">
				<view class="floatCartViewMainBg" />

				<view class="floatCartViewMainContent">
					<image class="icon" src="/static/cart-packagepng.png" @click="showShowPopup" />
					<view class="content">
						<text v-if="cartList.length === 0" class="empty">未选购商品</text>
						<block v-else>
							<text class="totalPrice">￥{{totalPrice}}</text>
						</block>

					</view>
					<view class="payBtn" :class=" [cartList.length === 0 ? 'payBtn--disable':'']">
						<text class="payBtnText" :class=" [cartList.length === 0 ? 'payBtnText--disable':'']">去下单</text>
					</view>
				</view>


			</view>

		</view>
		<!-- <uni-load-more :status="loadingType"></uni-load-more> -->
	</view>
</template>

<script>
	import uniNumberBox from '@/components/uni-number-box.vue';
	import HalfScreenPopup from '@/components/HalfScreenPopup.vue'
	import {
		fetchAllProductDetailList,
		fetchContent,
		fetchRecommendProductList
	} from '@/api/home.js';
	import {
		formatDate
	} from '@/utils/date';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import {

		fetchCartList,
		deletCartItem,
		updateQuantity,
		clearCartList
	} from '@/api/cart.js';
	export default {
		components: {
			HalfScreenPopup,
			uniNumberBox,
			uniLoadMore
		},
		data() {
			return {
				listModel: 'row',
				titleNViewBackground: '',
				titleNViewBackgroundList: ['rgb(203, 87, 60)', 'rgb(205, 215, 218)'],
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [],
				goodsList: [],
				advertiseList: [],
				homeFlashPromotion: [],
				recommendProductList: [],
				recommendParams: {
					pageNum: 1,
					pageSize: 4
				},
				loadingType: 'more',
				cartList: [],
				allProductList: [],
				totalPrice: "0",
				showPopup: false,
			};
		},
		onLoad() {
			this.loadData();
		},
		//下拉刷新
		onPullDownRefresh() {
			this.recommendParams.pageNum = 1;
			this.loadData();
		},
		//加载更多
		onReachBottom() {
			this.recommendParams.pageNum++;
			this.loadingType = 'loading';
			fetchRecommendProductList(this.recommendParams).then(response => {
				let addProductList = response.data;
				if (response.data.length === 0) {
					//没有更多了
					this.recommendParams.pageNum--;
					this.loadingType = 'nomore';
				} else {
					this.recommendProductList = this.recommendProductList.concat(addProductList);
					this.loadingType = 'more';
				}
			})
		},
		computed: {

			cutDownTime() {
				let endTime = new Date(this.homeFlashPromotion.endTime);
				let endDateTime = new Date();
				let startDateTime = new Date();
				endDateTime.setHours(endTime.getHours());
				endDateTime.setMinutes(endTime.getMinutes());
				endDateTime.setSeconds(endTime.getSeconds());
				let offsetTime = (endDateTime.getTime() - startDateTime.getTime());
				let endHour = Math.floor(offsetTime / (60 * 60 * 1000));
				let offsetMinute = offsetTime % (60 * 60 * 1000);
				let endMinute = Math.floor(offsetMinute / (60 * 1000));
				let offsetSecond = offsetTime % (60 * 1000);
				let endSecond = Math.floor(offsetSecond / 1000);
				return {
					endHour: endHour,
					endMinute: endMinute,
					endSecond: endSecond
				}
			}
		},
		filters: {
			formatTime(time) {
				if (time == null || time === '') {
					return 'N/A';
				}
				let date = new Date(time);
				return formatDate(date, 'hh:mm:ss')
			},
		},
		methods: {
			showShowPopup() {
				console.log("showShowPopup");
				this.showPopup = true
			},
			/**
			 * 加载数据
			 */
			async loadData() {
				fetchAllProductDetailList().then(response => {
					this.updateAllProductList(response.data)
				})

				fetchContent().then(response => {
					console.log("onLoad", response.data);
					this.advertiseList = response.data.advertiseList;
					this.swiperLength = this.advertiseList.length;
					this.titleNViewBackground = this.titleNViewBackgroundList[0];
					this.homeFlashPromotion = response.data.homeFlashPromotion;

					fetchRecommendProductList(this.recommendParams).then(response => {
						this.recommendProductList = response.data;
						uni.stopPullDownRefresh();
					})
					// this.updateAllProductList(response.data.newProductList)
				});
				fetchCartList().then(response => {
					this.cartList = response.data
					console.log("onLoad fetchCartList", response.data);
					this.updateAllProductList(this.allProductList)
				})

			},

			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				let changeIndex = index % this.titleNViewBackgroundList.length;
				this.titleNViewBackground = this.titleNViewBackgroundList[changeIndex];
			},
			//商品详情页
			navToDetailPage(item) {
				let id = item.id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
			//广告详情页
			navToAdvertisePage(item) {
				let id = item.id;
				console.log("navToAdvertisePage", item)
			},
			//更新商品列表
			updateAllProductList(list) {
				list.forEach(pItem => {
					pItem.quantity = 0
					for (let index = 0; index < this.cartList.length; index++) {
						let item = this.cartList[index];
						console.log("pItem.product.id = " + pItem.product.id + "item.productId = " + item.productId);
						if (pItem.product.id === item.productId) {
							pItem.quantity = item.quantity
							break;
						}
					}
				})


				this.allProductList = list;
				this.calcTotal()
			},
			//计算总价
			calcTotal() {
				let list = this.cartList;
				if (list.length === 0) {
					this.totalPrice = "0";
					return;
				}
				let totalPrice = 0;

				list.forEach(item => {
					totalPrice += item.price * item.quantity;
				})
				this.totalPrice = Number(totalPrice.toFixed(2));
			},
		},

		// #ifndef MP
		// 标题栏input搜索框点击
		onNavigationBarSearchInputClicked: async function(e) {
			this.$api.msg('点击了搜索框');
		},
		//点击导航栏 buttons 时触发
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.$api.msg('点击了扫描');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		}
		// #endif
	}
</script>

<style lang="scss">
	/* #ifdef MP */
	.mp-search-box {
		position: absolute;
		left: 0;
		top: 30upx;
		z-index: 9999;
		width: 100%;
		padding: 0 80upx;

		.ser-input {
			flex: 1;
			height: 56upx;
			line-height: 56upx;
			text-align: center;
			font-size: 28upx;
			color: $font-color-base;
			border-radius: 20px;
			background: rgba(255, 255, 255, .6);
		}
	}

	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
			margin-top: -20upx;
		}

		.carousel-section {
			padding: 0;

			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}

			.carousel {
				.carousel-item {
					padding: 0;
				}
			}

			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}

	/* #endif */


	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;

		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 426upx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	/* 分类 */
	.cate-section {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
		padding: 30upx 22upx;
		background: #fff;

		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
		}

		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88upx;
			height: 88upx;
			margin-bottom: 14upx;
			border-radius: 50%;
			opacity: .7;
			box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
		}
	}

	.ad-1 {
		width: 100%;
		height: 210upx;
		padding: 10upx 0;
		background: #fff;

		image {
			width: 100%;
			height: 100%;
		}
	}

	/* 秒杀专区 */
	.seckill-section {
		padding: 4upx 30upx 24upx;
		background: #fff;

		.s-header {
			display: flex;
			align-items: center;
			height: 92upx;
			line-height: 1;

			.s-img {
				width: 140upx;
				height: 30upx;
			}

			.tip {
				font-size: $font-base;
				color: $font-color-light;
				margin: 0 20upx 0 40upx;
			}

			.timer {
				display: inline-block;
				width: 40upx;
				height: 36upx;
				text-align: center;
				line-height: 36upx;
				margin-right: 14upx;
				font-size: $font-sm+2upx;
				color: #fff;
				border-radius: 2px;
				background: rgba(0, 0, 0, .8);
			}

			.icon-you {
				font-size: $font-lg;
				color: $font-color-light;
				flex: 1;
				text-align: right;
			}
		}

		.floor-list {
			white-space: nowrap;
		}

		.scoll-wrapper {
			display: flex;
			align-items: flex-start;
		}

		.floor-item {
			width: 300upx;
			margin-right: 20upx;
			font-size: $font-sm+2upx;
			color: $font-color-dark;
			line-height: 1.8;

			image {
				width: 300upx;
				height: 300upx;
				border-radius: 6upx;
			}

			.price {
				color: $uni-color-primary;
			}
		}

		.title2 {
			font-size: $font-sm;
			color: $font-color-light;
			line-height: 40upx;
		}
	}

	.f-header {
		display: flex;
		align-items: center;
		height: 140upx;
		padding: 6upx 30upx 8upx;
		background: #fff;

		image {
			flex-shrink: 0;
			width: 80upx;
			height: 80upx;
			margin-right: 20upx;
		}

		.tit-box {
			flex: 1;
			display: flex;
			flex-direction: column;
		}

		.tit {
			font-size: $font-lg +2upx;
			color: #font-color-dark;
			line-height: 1.3;
		}

		.tit2 {
			font-size: $font-sm;
			color: $font-color-light;
		}

		.icon-you {
			font-size: $font-lg +2upx;
			color: $font-color-light;
		}

		.timer {
			display: inline-block;
			width: 40upx;
			height: 36upx;
			text-align: center;
			line-height: 36upx;
			margin-right: 14upx;
			font-size: $font-sm+2upx;
			color: #fff;
			border-radius: 2px;
			background: rgba(0, 0, 0, .8);
		}
	}


	/* 猜你喜欢 */
	.guess-section {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;
		justify-content: space-between;

		.guess-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.image-wrapper-brand {
			width: 100%;
			height: 150upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}

		.title2 {
			font-size: $font-sm;
			color: $font-color-light;
			line-height: 40upx;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
		}
	}


	.hot-section {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;
		position: relative;

		.guess-item {
			display: flex;
			flex-direction: row;
			width: 100%;
			padding-bottom: 40upx;
		}

		.image-wrapper {
			width: 30%;
			height: 250upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}

		.title2 {
			font-size: $font-sm;
			color: $font-color-light;
			line-height: 40upx;
			height: 80upx;
			overflow: hidden;
			text-overflow: ellipsis;
			display: block;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 80upx;
		}

		.txt {
			position: relative;
			width: 70%;
			display: flex;
			flex-direction: column;
			padding-left: 40upx;
		}

		.selector-wrapper {
			width: 240upx;
			height: 70upx;
			position: absolute;
			bottom: 0upx;
			right: 0upx;
			display: flex;
			align-items: center;
			justify-items: right;


			.choose-btn {
				position: absolute;
				right: 0upx;
				background-color: #f5f5f5;
				padding: 10upx 20upx;
				font-size: 30upx;
			}

			.circle-num {
				position: absolute;
				top: -10upx;
				right: 0upx;
				min-width: 30rpx;
				height: 30rpx;
				line-height: 30rpx;
				background-color: $base-color;
				// border-radius: 50%;
				color: white;
				padding: 0 4px;
				text-align: center;
				font-size: 24rpx;
				border-radius: 999upx;
			}
		}


	}

	.floatCartView {
		position: fixed;
		bottom: 130upx;
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;

		.floatCartViewMain {


			align-items: center;
			justify-content: center;

			position: absolute;

			bottom: 0upx;

			width: 80%;
			margin: 0 30upx;

			.floatCartViewMainBg {
				width: 92%;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				height: 73upx;
				background-color: #323232;
				border-radius: 999upx;
				z-index: 0;
			}

			.floatCartViewMainContent {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				height: 75upx;

				.icon {
					width: 100upx;
					height: 100upx;
				}

				.content {
					position: relative;
					height: 100%;
					flex: 1;
					display: flex;
					flex-direction: row;
					align-items: center;

					.empty {
						font-weight: 700;
						font-size: 30upx;
						color: #b3b3b3;
					}

					.totalPrice {
						width: 100upx;
						font-weight: 700;
						color: white;
					}
				}

				.payBtn {
					display: flex;
					position: absolute;
					border-radius: 50upx;
					background-color: $base-color;
					height: 100%;
					width: 180upx;
					align-items: center;
					justify-content: center;
					right: 0upx;

					&.payBtn--disable {
						background-color: #595959;
					}

					.payBtnText {
						color: white;

						&.payBtnText--disable {
							color: #b3b3b3; // 改成你想要的文字颜色
						}
					}
				}

			}

		}
	}

	// .Popup {
	// 	z-index: 0;
	// }
</style>